<?php

App::uses('Component', 'Controller');
App::uses('GatewayFlowComponent', PAYMENT_CAMELCASENAME.'.Component');
require_once 'GatewayFlowComponent.php';

class PagseguroComponent extends Component implements GatewayFlowComponent{
	
	var $components = array('Session');
	var $data;
	var $transaction_url = 'https://ws.pagseguro.uol.com.br/v2/checkout';
	var $notification_url = 'https://ws.pagseguro.uol.com.br/v2/transactions/notifications/';
	var $payment_link = 'https://pagseguro.uol.com.br/v2/checkout/payment.html?code=';
	
	public function __construct(ComponentCollection $collection, $settings = array()){
		$this->components[PAYMENT_CAMELCASENAME.'.Curl'] = array('url'=>'');
		parent::__construct($collection, $settings);
	}
	
	public function config($id){
		$model = ClassRegistry::init(PAYMENT_CAMELCASENAME.'.PaymentGateway');
		return $model->config($id);
	}
	
	public function setUpTransaction($id, $reference, $items){
		$this->Curl->setUrl($this->transaction_url);
		$config = $this->config($id);
		$this->Curl->setOption(CURLOPT_POST, true);
		$this->Curl->setOption(CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8"));
		$this->Curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
		//$this->data['email'] = 'willen.goulart@gmail.com';
		//$this->data['token'] = 'AAEDC2A6D4C24CF29117A19FAFCA44D8';
		$this->data['email'] = $config['email'];
		$this->data['token'] = $config['token'];
		$this->data['reference'] = $reference;
		$this->data['redirectURL'] = Router::url(array('controller'=>'payment', 'action'=>'gateway_return'), true);
		$this->data['notificationURL'] = Router::url(array('controller'=>'payment', 'action'=>'notification', 'Pagseguro'), true);
		$this->data['currency'] = 'BRL';
		$this->num_itens = 0;
		foreach($items as $item){
			$this->addProduto($item);
			
		}
		$this->Curl->setData(http_build_query($this->data));
	}
	
	public function addProduto($data){
		$reg = ++$this->num_itens;
		$this->data['itemId'.$reg] = $data['id'];
		$this->data['itemDescription'.$reg] = $data['description'];
		$this->data['itemAmount'.$reg] = $data['amount'];
		$this->data['itemQuantity'.$reg] = $data['quantity'];
	}
	
	public function sendTransaction(){
		return $this->testReturn($this->Curl->sendTransaction());
	}
	
	public function testReturn($return){
		if($return == 'Unauthorized'){
			$this->Session->setFlash(__('As credenciais de conexão ao PagSeguro estão incorretas!'));
			return;
		}
		$xml= simplexml_load_string($return);
		if(isset($xml->error))
			$this->Session->setFlash($xml->error->code. ' - '.$xml->error->message);
		else{
			return array('code'=>$this->payment_link.$xml->code, 'date'=>$xml->date);
		}
	}
	
	public function notification($data){
		return array(
			'notification_code'=>$data['notificationCode'],
			'notification_type'=>$data['notificationType'],
			'new'=>1
		);
	}
	
	public function getNotification($notification, $gateway_id){
		$config = $this->config($gateway_id);
		$this->Curl->setUrl($this->notification_url.$notification.'?'.
				'email='.$config['email'].'&token='.$config['token']);
		$return = ($this->Curl->sendTransaction());
		return simplexml_load_string($return);
	}
	
}
